package com.example.order.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import com.example.order.pojo.Student;
import com.example.order.service.StockFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author JFeng
 * @Date 2024/4/19
 */

@RestController
@RequestMapping("/order")
@RefreshScope // 修改配置中心的配置后，会刷新配置
public class OrderController {
//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private StockFeignService stockFeignService;

    @Value("${user.name}")
    private String userName;

    @Value("${user.age}")
    private String age;

    @Value("${server.port}")
    private String port;


    @RequestMapping("/add")
    public Object add() {
        String response = stockFeignService.reduce((int) Math.random());
//        ResponseEntity<Object> response = restTemplate.getForEntity("http://stock-service/stock/reduce", Object.class);
        Map<Object, Object> map = new HashMap<>();
        map.put("stockService", response);
        map.put("orderService", UUID.fastUUID().toString());
        map.put("userName",userName);
        map.put("age",age);
        map.put("port",port);

        return JSONUtil.toJsonStr(map);
    }



    @RequestMapping("/test")
    public Object test(@RequestBody Student student) {

        return student;
    }
}
